﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Weixin.MP.BLL.Users;
using Weixin.MP.DAL.WeixinMP;
using Weixin.MP.UI;

namespace Weixin.MP.Web.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        public ActionResult Index()
        {
            if (Session[Common.LoginUserSession] != null)
            {
                return RedirectToAction("Index", "Home");
            }
            return View();
        }

        [HttpPost]
        public ActionResult Index(string username, string password, string code)
        {

            if (HttpContext.Session[Common.VerifyCodeSession] == null)
            {
                return Content("验证码错误！");
            }
            if (!code.Equals(HttpContext.Session[Common.VerifyCodeSession].ToString(), StringComparison.CurrentCultureIgnoreCase))
            {
                return Content("验证码错误！");
            }
            var bo = new UsersBO();
            var loginUser = bo.GetUsers(username, password.ToMD5());
            //var loginUser = bo.GetUsers("admin", "admin".ToMD5());
            if (loginUser == null)
            {

                return Content("用户名或密码错误！");

            }
            loginUser.LoginTime = DateTime.Now;
            //修改最后一次登录时间
            bo.UpdateLoginTime(username, loginUser.LoginTime);
            //写登录日志
            bo.AddOperateLog(new OperateLogEO()
            {
                ClientIp = Common.GetClientIP(),
                LogType = OperateLogEO.LogTypeEnum.Login,
                UserId = loginUser.UserId,
                LogDate = DateTime.Now,
                Content = "用户登录"
            });

            Session[Common.LoginUserSession] = loginUser;
            return Content("ok");
            //return RedirectToAction("Index","Home");
        }

    }
}
